package com.xjh;

import com.xjh.uitl.StudentDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.List;

public class Index extends JFrame implements MouseListener {

    JLabel name = new JLabel("姓名：");
    JLabel age = new JLabel("年龄：");
    JLabel address = new JLabel("地址：");
    JLabel password = new JLabel("密码：");


    JTextField nameText = new JTextField();
    JTextField ageText = new JTextField();
    JTextField addressText = new JTextField();
    JPasswordField passwordText = new JPasswordField();

    JButton save = new JButton("添加");
    JButton delete = new JButton("删除");
    JButton update = new JButton("修改");
    JButton replace = new JButton("重置");


    JTable jTable;

    StudentDao sd = new StudentDao();


    public Index() throws SQLException, ClassNotFoundException {
        //初始化窗口
        initJframe();

        //初始化组件
        initView();


        this.setVisible(true);
    }

    private void initJframe() {
        //设置界面的宽高
        this.setSize(603, 680);
        //设置界面的标题
        this.setTitle("用户管理系统");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认的居中放置，只有取消了才会按照XY轴的形式添加组件
        this.setLayout(null);
        //给整个界面添加键盘监听事件

    }

    private void initView() throws SQLException {

        Font font = new Font("仿宋", Font.PLAIN, 20);
        name.setFont(font);
        age.setFont(font);
        password.setFont(font);
        address.setFont(font);

        nameText.setFont(font);
        ageText.setFont(font);
        addressText.setFont(font);
        passwordText.setFont(font);

        name.setBounds(100, 400, 100, 100);
        age.setBounds(300, 400, 100, 100);

        password.setBounds(300, 450, 100, 100);
        address.setBounds(100, 450, 100, 100);


        nameText.setBounds(160, 440, 120, 20);
        ageText.setBounds(360, 440, 120, 20);
        addressText.setBounds(160, 490, 120, 20);
        passwordText.setBounds(360, 490, 120, 20);


        save.setBorderPainted(false);
        save.setFont(new java.awt.Font("仿宋", 1, 15));
        save.setBackground(Color.lightGray);
        save.setBounds(100, 550, 70, 30);

        delete.setBorderPainted(false);
        delete.setFont(new java.awt.Font("仿宋", 1, 15));
        delete.setBackground(Color.lightGray);
        delete.setBounds(200, 550, 70, 30);

        update.setBorderPainted(false);
        update.setFont(new java.awt.Font("仿宋", 1, 15));
        update.setBackground(Color.lightGray);
        update.setBounds(300, 550, 70, 30);

        replace.setBorderPainted(false);
        replace.setFont(new java.awt.Font("仿宋", 1, 15));
        replace.setBackground(Color.lightGray);
        replace.setBounds(400, 550, 70, 30);


        String[] columnNames =
                {"姓名", "年龄", "地址"};

        List<Student> list = sd.getStudent();
        Object[][] obj = new Object[100][3];


        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < 3; j++) {
                Student s = list.get(i);
                switch (j) {
                    case 0:
                        obj[i][j] = s.getName();
                        break;
                    case 1:
                        obj[i][j] = s.getAge();
                        break;
                    case 2:
                        obj[i][j] = s.getAddress();
                        break;
                }
            }
        }


        jTable = new JTable(obj, columnNames);
        jTable.setBounds(0, 20, 600, 400);
        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);


        save.addMouseListener(this);
        replace.addMouseListener(this);
        update.addMouseListener(this);
        delete.addMouseListener(this);


        this.getContentPane().add(jTable);
        this.getContentPane().add(name);
        this.getContentPane().add(password);
        this.getContentPane().add(address);
        this.getContentPane().add(age);
        this.getContentPane().add(nameText);
        this.getContentPane().add(ageText);
        this.getContentPane().add(addressText);
        this.getContentPane().add(passwordText);
        this.getContentPane().add(save);
        this.getContentPane().add(delete);
        this.getContentPane().add(update);
        this.getContentPane().add(replace);


    }


    @Override
    public void mouseClicked(MouseEvent e) {
        //鼠标点击事件
        Object source = e.getSource();

        if (source == save) {
            System.out.println("点击了添加按钮");
            String name = nameText.getText();
            String address = addressText.getText();
            String age = ageText.getText();
            String password = passwordText.getText();


            if (password!=null&&password.length()>0){
                //说明输入了密码
                Student stu=new Student(name,Integer.parseInt(age),address,password);
                int count = sd.insert(stu);
                if (count>=1){
                    System.out.println("插入成功");
                    try {
                        initView();
                        this.getContentPane().repaint();
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                }

            }else {
                //密码是空的，弹出弹窗
                System.out.println("密码为空");
                showJDialog("密码不能为空");
            }


        } else if (source == update) {
            System.out.println("点击了修改按钮");
            String name = nameText.getText();
            String address = addressText.getText();
            String age = ageText.getText();
            String password = passwordText.getText();

            if (password!=null&&password.length()>0){
                Student stu=new Student(name,Integer.parseInt(age),address,password);
                try {
                    int count = sd.update(stu);
                    if (count>0){
                        System.out.println("更新成功");
                    }else {
                        System.out.println("更新失败");
                    }

                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }

            }else {
                showJDialog("密码不能为空");
            }





        } else if (source == delete) {
            System.out.println("点击了删除按钮");
            String password = passwordText.getText();
            String name = nameText.getText();

            if (password!=null&&password.length()>0){
                System.out.println("删除数据"+name);
                sd.deleteByName(name,password);


            }else {
                showJDialog("请输入密码");
            }




        } else if (source == replace) {
            System.out.println("点击了重置按钮");

            this.ageText.setText("");
             this.passwordText.setText("");
            this.nameText.setText("");
            this.addressText.setText("");

            this.getContentPane().repaint();

        }



    }

    public void showJDialog(String content) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(200, 150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);

        //创建Jlabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        //让弹框展示出来
        jDialog.setVisible(true);
    }
    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

